﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
	Group_subject	:	งานทุกอย่างที่เกี่ยวกับ กลุ่มรายวิชา
	เพิ่ม ลบ แก้ไข
\												/


*/

$(function()
{
	getgopdata();
});

function getgopdata()
{
	$('#workarea').html('<tr><th width="30">Id</th><th width="200">Deparment Name</th><th width="30">Number</th><th width="400">Name</th><th width="50">Edit</th><th width="50">Delete</th></tr>');
	$.getJSON('class/_reqgop.php',function(data)
	{
		$.each(data,function(key,val){
		$('#workarea').html($('#workarea').html()+'<tr id="data'+val['id']+'"><td id="id'+val['id']+'">'+ val['id']+'</td><td id="dname'+val['id']+'"><input type="hidden" id="did'+val['id']+'" value="'+val['did']+'">'+ val['dName']+'</td><td id="number'+val['id']+'">'+ val['number']+'</td><td id="name'+val['id']+'">'+val['name']+ '</td><td id="edit'+val['id']+'"><li class="ui-state-default ui-corner-all" id="editbutton'+val['id']+'" title="Edit" onClick="editgopdata('+val['id']+')"><span class="ui-icon ui-icon-pencil" ></span></li></td><td><li class="ui-state-default ui-corner-all" id="delbutton'+val['id']+'" title="Delete" onClick="gopdelcon('+val['id']+');"><span class="ui-icon ui-icon-trash"></span></li></td></tr>');
		//$('#id'+val['id']).button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#dname'+val['id']).button().css({'cursor': 'default','width': '200','height': '20'});
		//$('#number'+val['id']).button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#name'+val['id']).button().css({'cursor': 'default','width': '400','height': '20'});
		$('#editbutton'+val['id']).button().css({'cursor': 'pointer'});
		$('#delbutton'+val['id']).button().css({'cursor': 'pointer'});
		});
		$('#workarea').html($('#workarea').html()+'<tr id="dataadd"><td id="idadd">ADD</td><td id="selectadd"><select id="selectdepadd"></select></td><td id="numberadd"><input type="text"  value="" id="txtnumberadd" size="3"></td><td id="nameadd"><input type="text"  value="" id="txtnameadd" size="50"></td><td colspan="2"><li class="ui-state-default ui-corner-all" id="addbutton" title="Add" onClick="gopadd();"><span class="ui-icon ui-icon-check"></span></li></td></tr>');
		//$('#idadd').button().css({'width': '30','height': '20'});
		depselect('#selectdepadd');
		//$('#selectadd').button().css({'cursor': 'default','width': '200','height': '20'});
		//$('#numberadd').button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#nameadd').button().css({'cursor': 'default','width': '400','height': '20'});
		$('#addbutton').button().css({'cursor': 'pointer','width': '100'});
	});
}

function depselect(where,id)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqdep.php',function(data)
		{
			$.each(data,function(key,val){
				if(val['id'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'" selected="selected">'+val['number']+' - '+val['name']+'</option>');
				} else 
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
				}
			});
		});
}

function gopadd()
{
	if($('#txtnumberadd').val() == ''&&$('#txtnameadd').val() == '')
	{
		$('#dialog').html('Plase enter Number and Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectdepadd').val() == '')
	{
		$('#dialog').html('Plase enter the Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnumberadd').val() == '')
	{
		$('#dialog').html('Plase enter the Number');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnameadd').val() == '')
	{
		$('#dialog').html('Plase enter the Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqgopinsert.php',{'did':$('#selectdepadd').val(),'number':$('#txtnumberadd').val(),'name':$('#txtnameadd').val()},function(data)
		{
			$('#dialog').html('Insert Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			$('#txtnumberadd').val('');
			$('#txtnameadd').val('');
			getgopdata()
		});
	}	
}

function editgopdata(id)
{
	var did = $('#did'+id).val();
	$('#dname'+id).html('<select id="selectdep'+id+'"></select>');
	depselect('#selectdep'+id,did);
	$('#number'+id).html('<input type="text"  value="'+$('#number'+id).text()+'" id="txtnumber'+id+'" size="3">');
	$('#name'+id).html('<input type="text"  value="'+$('#name'+id).text()+'" id="txtname'+id+'" size="50">');
	$('#edit'+id).html('<li class="ui-state-default ui-corner-all" title="Enter" onClick="gopupdate('+id+');" id="enterbutton'+id+'"><span class="ui-icon ui-icon-check"></span></li>');
	$('#enterbutton'+id).button().css({'cursor': 'pointer','width': '50'});
}

function gopupdate(id)
{
	if($('#txtnumber'+id).val() == ''&&$('#txtname'+id).val() == '')
	{
		$('#dialog').html('Plase enter Number and Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectdep'+id).val() == '')
	{
		$('#dialog').html('Plase enter the Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnumber'+id).val() == '')
	{
		$('#dialog').html('Plase enter the Number');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtname'+id).val() == '')
	{
		$('#dialog').html('Plase enter the Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqgopupdate.php',{'id':id,'did':$('#selectdep'+id).val(),'number':$('#txtnumber'+id).val(),'name':$('#txtname'+id).val()},function(data)
		{
			$('#dname'+id).html($('#selectdep'+id+' option:selected').text());
			$('#number'+id).html($('#txtnumber'+id).val());
			$('#name'+id).html($('#txtname'+id).val());
			$('#edit'+id).html('<li class="ui-state-default ui-corner-all" title="Edit" onClick="editgopdata('+id+');" id="editbutton'+id+'"><span class="ui-icon ui-icon-pencil"></span></li>');
			$('#editbutton'+id).button().css({'cursor': 'pointer','width': '50'});
			$('#dialog').html('Edit Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
		});
	}
}

function gopdelcon(id)
{
	$('#delcon').html('You want to Delete this?');
		$('#delcon').dialog({ buttons: [{text: 'Delete',click: function()
		{ 
			gopdeldata(id);
			$( this ).dialog('close'); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
		$('#delcon').dialog({'title':'Confirm Delete'});
		$('#delcon').dialog('option','resizable',false);
}

function gopdeldata(id)
{
	$.get('class/_reqgopdel.php',{'id':id},function(data)
	{
		$('#dialog').html('Delete Success');
		$('#dialog').dialog({'title':'Success'});
		$('#dialog').dialog('option','resizable',false);
		$('#data'+id).hide();
	});
}